package com.edward.action;

import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.struts2.ServletActionContext;
import org.springframework.security.annotation.Secured;

import com.edward.JsonDateProcessor;
import com.edward.PagerModel;
import com.edward.model.ExistentInvoice;
import com.edward.model.UnSubmit;
import com.edward.service.UnSubmitManager;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UnSubmitAction extends ActionSupport{

	
	private static final long serialVersionUID = -7739345050229310360L;

	private ExistentInvoice existentInvoice;
	
	private UnSubmit unSubmit;
	
	private UnSubmitManager unSubmitManager;
	
	private String datas="";
	
	private int limit;
	
	private int start;


	public ExistentInvoice getExistentInvoice() {
		return existentInvoice;
	}

	public void setExistentInvoice(ExistentInvoice existentInvoice) {
		this.existentInvoice = existentInvoice;
	}

	public UnSubmit getUnSubmit() {
		return unSubmit;
	}

	public void setUnSubmit(UnSubmit unSubmit) {
		this.unSubmit = unSubmit;
	}


	public UnSubmitManager getUnSubmitManager() {
		return unSubmitManager;
	}

	public void setUnSubmitManager(UnSubmitManager unSubmitManager) {
		this.unSubmitManager = unSubmitManager;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}
	
	public String getDatas() {
		return datas;
	}

	public void setDatas(String datas) {
		this.datas = datas;
	}

	@Secured("ROLE_FINANCE")
	public String add() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		unSubmitManager.addUnSubmit(unSubmit);
		int total = unSubmitManager.listUnSubmit().size();
		response.getWriter().print("{success : true , msg : '添加成功!',unSubmit_id:"+ unSubmit.getId()+",total:" + total + "}");
		return null;
	}
	
	@Secured("ROLE_FINANCE")
	public String update() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		UnSubmit u = unSubmitManager.getUnSubmit(unSubmit.getId());
		unSubmit.setMoney(u.getMoney());
		unSubmitManager.updateUnSubmit(unSubmit);
		response.getWriter().print("{success : true , msg : '修改成功!'}");
		return null;
	}
	
	@Secured("ROLE_FINANCE")
	public String delete() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		UnSubmit u = unSubmitManager.getUnSubmit(unSubmit.getId());
		if(unSubmitManager.getEIList(u.getId()).size() != 0){
			response.getWriter().print("{success : false , msg : '删除失败，存在货物列表，请先删除货物!'}");
		}else{
			unSubmitManager.deleteUnSubmit(u.getId());
			response.getWriter().print("{success : true , msg : '删除成功!'}");
		}
		return null;
	}

	@Secured("ROLE_FINANCE")
	public String getList() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
	    JsonConfig cfg = new JsonConfig();
		cfg.registerJsonValueProcessor(java.util.Date.class, new JsonDateProcessor());
		cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonDateProcessor());
		PagerModel pager;
		pager = unSubmitManager.pagerList(start, limit);
		JSONObject jsonObject = JSONObject.fromObject(pager,cfg);
		String jsonStr = jsonObject.toString();
		response.getWriter().print(jsonStr);
		return null;
	}
	
	@Secured("ROLE_FINANCE")
	public String addItem() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		UnSubmit u  = unSubmitManager.getUnSubmit(unSubmit.getId());
		u.setMoney(u.getMoney() + existentInvoice.getMoney());
		unSubmitManager.updateUnSubmit(u);
		existentInvoice.setUnSubmit(u);
		if(existentInvoice.getId() == 0){
			unSubmitManager.addEI(existentInvoice);
		}else{
		 	ExistentInvoice	ei = unSubmitManager.getEI(existentInvoice.getId());
	     	existentInvoice.setCompany(ei.getCompany());
	     	existentInvoice.setDate(ei.getDate());
	     	existentInvoice.setCount(ei.getCount());
	     	existentInvoice.setInvoiceNum(ei.getInvoiceNum());
			unSubmitManager.updateEI(existentInvoice);
		}
		response.getWriter().print("{success : true , msg : '添加成功!'}");
		return null;
	}
	
	@Secured("ROLE_FINANCE")
	public String updateItem() throws Exception {
	
			ActionContext ctx = ActionContext.getContext();        
			  HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
			response.setContentType("text/html;charset=UTF-8");
			UnSubmit u  = unSubmitManager.getUnSubmit(unSubmit.getId());
			ExistentInvoice	ei = unSubmitManager.getEI(existentInvoice.getId());
			u.setMoney(u.getMoney() + existentInvoice.getMoney() - ei.getMoney());
	     	existentInvoice.setCompany(ei.getCompany());
	     	existentInvoice.setDate(ei.getDate());
	     	existentInvoice.setCount(ei.getCount());
	     	existentInvoice.setInvoiceNum(ei.getInvoiceNum());
	     	existentInvoice.setFlag("t");
	     	existentInvoice.setUnSubmit(u);
			unSubmitManager.updateEI(existentInvoice);
			unSubmitManager.updateUnSubmit(u);
			response.getWriter().print("{success : true , msg : '修改成功!'}");
			return null;
		}
	
	@Secured("ROLE_FINANCE")
	public String deleteItem() throws Exception {
	
			ActionContext ctx = ActionContext.getContext();        
			HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
			response.setContentType("text/html;charset=UTF-8");
			ExistentInvoice ei = unSubmitManager.getEI(existentInvoice.getId());
			UnSubmit u  = unSubmitManager.getUnSubmit(unSubmit.getId());
			u.setMoney(u.getMoney() - ei.getMoney());
			unSubmitManager.deleteEI(ei);
			unSubmitManager.updateUnSubmit(u);
			response.getWriter().print("{success : true , msg : '删除成功'}");
			return null;
		}
	
	@Secured("ROLE_FINANCE")
	public String getItems() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		JsonConfig cfg = new JsonConfig();
		cfg.registerJsonValueProcessor(java.util.Date.class, new JsonDateProcessor());
		cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonDateProcessor());
		PagerModel pager;
		if(datas.equals("")){
			  pager = unSubmitManager.pagerEI(start, limit,unSubmit.getId());
		}else{
			String id[] = datas.split(",");
			 pager = unSubmitManager.pagerEI(start, limit,id);
		}
	  
		JSONObject jsonObject = JSONObject.fromObject(pager,cfg);
		String jsonStr = jsonObject.toString();
		response.getWriter().print(jsonStr);
		return null;
	}
	
	@Secured("ROLE_FINANCE")
	 public String getListJudge() throws Exception {
			ActionContext ctx = ActionContext.getContext();        
			HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
			response.setContentType("text/html;charset=UTF-8");
				response.getWriter().print("{success : true , msg : '成功!',deny:false}");
		 return null;
	 }
	
	
	
	
}
